package co.yixiang.hw.service.impl;

import co.yixiang.hw.domain.YxStoreCoupon;
import co.yixiang.hw.domain.YxStoreCouponIssueUser;
import co.yixiang.hw.domain.YxStoreCouponUser;
import co.yixiang.hw.repository.YxStoreCouponUserRepository;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.hw.repository.YxStoreCouponIssueUserRepository;
import co.yixiang.hw.service.YxStoreCouponIssueUserService;
import co.yixiang.hw.service.dto.YxStoreCouponIssueUserDto;
import co.yixiang.hw.service.dto.YxStoreCouponIssueUserQueryCriteria;
import co.yixiang.hw.service.mapper.YxStoreCouponIssueUserMapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import javax.annotation.Resource;


/**
* @author Walert
* @date 2020-03-10
*/
@Service
@CacheConfig(cacheNames = "yxStoreCouponIssueUser")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStoreCouponIssueUserServiceImpl implements YxStoreCouponIssueUserService {

    @Resource
    private YxStoreCouponIssueUserRepository yxStoreCouponIssueUserRepository;
    @Resource
    private YxStoreCouponIssueUserMapper yxStoreCouponIssueUserMapper;

    @Resource
    private YxStoreCouponUserRepository couponUserRepository;


    @Override
    //@Cacheable
    public Map<String,Object> queryAll(YxStoreCouponIssueUserQueryCriteria criteria, Pageable pageable){
        Page<YxStoreCouponIssueUser> page = yxStoreCouponIssueUserRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(yxStoreCouponIssueUserMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<YxStoreCouponIssueUserDto> queryAll(YxStoreCouponIssueUserQueryCriteria criteria){
        return yxStoreCouponIssueUserMapper.toDto(yxStoreCouponIssueUserRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public YxStoreCouponIssueUserDto findById(Integer id) {
        YxStoreCouponIssueUser yxStoreCouponIssueUser = yxStoreCouponIssueUserRepository.findById(id).orElseGet(YxStoreCouponIssueUser::new);
        ValidationUtil.isNull(yxStoreCouponIssueUser.getId(),"YxStoreCouponIssueUser","id",id);
        return yxStoreCouponIssueUserMapper.toDto(yxStoreCouponIssueUser);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public YxStoreCouponIssueUserDto create(YxStoreCouponIssueUser resources) {
        return yxStoreCouponIssueUserMapper.toDto(yxStoreCouponIssueUserRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(YxStoreCouponIssueUser resources) {
        YxStoreCouponIssueUser yxStoreCouponIssueUser = yxStoreCouponIssueUserRepository.findById(resources.getId()).orElseGet(YxStoreCouponIssueUser::new);
        ValidationUtil.isNull( yxStoreCouponIssueUser.getId(),"YxStoreCouponIssueUser","id",resources.getId());
        yxStoreCouponIssueUser.copy(resources);
        yxStoreCouponIssueUserRepository.save(yxStoreCouponIssueUser);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            yxStoreCouponIssueUserRepository.deleteById(id);
        }
    }
    
    @Override
    @Cacheable(value = "issueCouponUser",key = "'uid-'+ #p0 + '_icid' + #p1" ,unless = "#result == false")
    public boolean checkByUidAndIssueCouponId(Integer uid, Integer issueCouponId) {
        List<YxStoreCouponUser> list = couponUserRepository.getByUidAndIssueCouponIdAndStatus(uid, issueCouponId,0);
        if(list != null && list.size() > 0){
            return true;
        }
        return false;
    }
}